package com.blog.zip.controller.web;

import com.blog.zip.mapper.ArticleMapper;
import com.blog.zip.mapper.CategoryMapper;

import com.blog.zip.mapper.TagMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.*;

@Slf4j
@RestController
public class WebTag {

  @Autowired
  private CategoryMapper categoryMapper;

  @Autowired
  private ArticleMapper articleMapper;

  @Autowired
  private TagMapper tagMapper;

  /**
   * 通过标签搜索文章
   *
   * @param m
   * @return
   */
  @PostMapping("/tag/article/get")
  public Map<String, Object> getCategory(@RequestBody Map<String, Object> m) {
    List<Map<String, Object>> list = new ArrayList<>();
    list = tagMapper.getTagArticle((String) m.get("tagName"));
    list.removeIf(article -> article.get("articleTitle") == null);
    Map<String, Object> map = new HashMap();
    map.put("code", 200);
    map.put("data", list);
    return map;
  }

  /**
   * 获取所有的标签，以及个数
   */
  @GetMapping("/tags/get")
  public Map<String, Object> getTags() {
    List<Map<String, Object>> list = new ArrayList<>();
    list = tagMapper.getAllTag();
    for (Map<String, Object> m : list) {
      String str = (String) m.get("title");
      List tags = Arrays.asList(str.split(","));
      m.put("title", tags.get(0));
      m.put("size", tags.size());
    }
    Map<String, Object> map = new HashMap();
    map.put("code", 200);
    map.put("data", list);
    return map;
  }
}
